var express = require('express');
var router = express.Router();
var wx = require('../controller/WxController');
var usr = require('../controller/UserController');
var prod = require('../controller/ProdController');
var order = require('../controller/OrderController');
var home = require('../controller/HomeController');
var comm = require('../controller/CommController');
var share = require('../controller/ShareController');
var depos = require('../controller/DeposController');
var coupon = require('../controller/CouponController');
var wxutils = require("../utils/WxUtil");
var logger = require('../lib/log').logger;

/* GET home page. */
router.use('/:class/:func', function(req, res, next) {
// router.use('/index', function(req, res, next) {
    console.log('URI /'+req.params.class+'/'+req.params.func);
    wxutils.verifyUser(req, res, next,function (req,res,next,err,result) {
        if(err){
            logger.debug(err.url);
            return res.redirect(err.url);
        }else {
            try {
                switch (req.params.class) {
                    case 'wx' :
                        wx[req.params.func](req, res, next);
                        break;
                    case 'usr':
                        usr[req.params.func](req, res, next);
                        break;
                    case 'prod':
                        prod[req.params.func](req, res, next);
                        break;
                    case 'order':
                        order[req.params.func](req, res, next);
                        break;
                    case 'comm':
                        comm[req.params.func](req, res, next);
                        break;
                    case 'share':
                        share[req.params.func](req, res, next);
                        break;
                    case 'depos':
                        depos[req.params.func](req, res, next);
                        break;
                    case 'coupon':
                        coupon[req.params.func](req, res, next);
                        break;
                    default:
                        home['index'](req, res, next);
                }
            }catch (err){
                logger.error(err);
                home['index'](req, res, next);
            }
        }
    });
});

module.exports = router;
